<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 主体 -->
    <el-card class="box-card">
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queryinfo.query"
            clearable
            @input="seach"
            @clear="seach"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="seach"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="10" class="clo">
          <el-button type="primary" @click="$router.push('/add')"
            >添加商品</el-button
          >
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="table" border>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="680">
        </el-table-column>
        <el-table-column prop="goods_price" label="商品价格（元）">
        </el-table-column>
        <el-table-column prop="goods_number" label="商品重量">
        </el-table-column>
        <el-table-column :formatter="formatter" label="创建时间" width="180">
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
            ></el-button>
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryinfo.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="queryinfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import _ from 'lodash'
import { goodsdel, goodslist } from '../http/api'
var moment = require('moment')

export default {
  data() {
    return {
      table: [],
      queryinfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      total: 0
    }
  },
  created() {
    this.list()
  },
  methods: {
    handleSizeChange(val) {
      this.queryinfo.pagesize = val
      this.list()
    },
    handleCurrentChange(val) {
      this.queryinfo.pagenum = val
      this.list()
    },
    //   获取商品列表
    async list() {
      const res = await goodslist(this.queryinfo)
      const { goods, total } = res
      this.table = goods
      this.total = total
    },
    // 时间戳
    formatter(row) {
      return moment(row.upd_time * 1000).format('YYYY-MM-DD HH:mm:ss')
    },
    // 删除
    async del(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await goodsdel(id)
          this.list()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 搜索防抖
    seach: _.debounce(function () {
      this.list()
    }, 1000)
  }
}
</script>

<style lang="less" scoped>
.clo {
  margin-left: 20px;
}
.el-table {
  margin-top: 16px;
}
</style>
